package heima_bixiangdong.itcast;

/**
 * 线程任务 --- 并发卖票(当然是票自己最清楚自己还剩多少张)(多个线程需要并发执行这个卖票任务)
 */
public class Ticket implements Runnable {

    // 票的数量
    private int tickets;

    public Ticket(int tickets) {
        this.tickets = tickets;
    }

    public int getTickets() {
        return tickets;
    }

    public void setTickets(int tickets) {
        this.tickets = tickets;
    }

    // 没有加同步，多线程并发执行会出现问题
    @Override
    public void run() {
        while (tickets > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "..." + tickets--);
        }
    }
}
